import axios from "axios";
import qs from "querystringify";

let apiContextPath = "";
const onlineDevelopmentMode =
  process.env.VUE_APP_ONLINE_DEVELOPMENT_MODE === "true" ||
  process.env.REACT_APP_ONLINE_DEVELOPMENT_MODE === "true";
if (process.env.NODE_ENV === "development" && !onlineDevelopmentMode) {
  apiContextPath = "/api";
}
export const getInstance = (prefix = "") => {
  if (prefix) {
    prefix.startsWith("/") && (prefix = prefix.slice(1, prefix.length));
    prefix.endsWith("/") && (prefix = prefix.slice(0, -1));
  }
  const instance = axios.create({
    baseURL: `${apiContextPath}/${prefix ? prefix + "/" : ""}sdata/rest`,
    timeout: 60000,
    validateStatus: function (status) {
      return status >= 200 && status < 300; // default
    },
    headers:
      (window.location.search &&
        new URLSearchParams(window.location.search).get("token")) ||
      window.token
        ? {
            token:
              new URLSearchParams(window.location.search).get("token") ||
              window.token,
          }
        : {},
  });

  instance.defaults.headers.post["Content-Type"] = "application/json";

  instance.interceptors.response.use(
    (response) => {
      let { data } = response;
      if (typeof data === "string") {
        data = JSON.parse(data);
      }
      if (data && data.status !== 200 && !(data instanceof Blob)) {
        return Promise.reject(response);
      }
      if (data instanceof Blob) {
        response.data = data;
        return response;
      }

      response.data = data && data.result;
      return response;
    },
    (error) => {
      if (error.response && error.response.status === 401) {
        return;
      }

      return Promise.reject(error.response);
    }
  );
  return instance;
};
let prefixPath = window.prefixPath || "";

export default getInstance(prefixPath);
